﻿''' <summary>
''' Provides a collection of GameObjects keyed by name which can be sorted by z-order.
''' </summary>
''' <remarks></remarks>
Public Class GameObjectCollection
    Inherits System.Collections.ObjectModel.KeyedCollection(Of String, GameObject)

    Protected Overrides Function GetKeyForItem(ByVal item As GameObject) As String
        Return item.Name
    End Function

    ''' <summary>
    ''' Adds a group of GameObjects to the collection.
    ''' </summary>
    ''' <param name="items">The IEnumerable source of GameObjects to add.</param>
    ''' <remarks></remarks>
    Public Sub AddRange(items As IEnumerable(Of GameObject))
        For Each entry In items
            Add(entry)
        Next
    End Sub

    ''' <summary>
    ''' Returns a GameObject array of this collection's contents, sorted by z-order.
    ''' </summary>
    ''' <returns>An array of GameObject sorted by z-order.</returns>
    ''' <remarks></remarks>
    Public Function ToZOrderedArray() As GameObject()
        If Count > 0 Then
            Dim result(Count - 1) As GameObject
            CopyTo(result, 0)
            Array.Sort(result, Function(source As GameObject, other As GameObject) source.ZOrder.CompareTo(other.ZOrder))
            Return result
        End If
        Return New GameObject() {}
    End Function
End Class
